limsClient.editPage = {

    getGridParam: function () {
        var _this = this
        return {
            type: 'grid',
            id: _this.idManager.clientGrid,
            url: limsClient.baseUrl + '/queries',
            service: 'limsClientServiceImpl',
            dbTable: 'T_LIMS_CLIENT',
            page: true,
            columns: limsClient.getEditListColumns(),
            onLoadSuccess: function (data) {
                if (!Gikam.isEmpty(data)) {
                    this.activeRowByIndex(0)
                } else {
                    Gikam.getAsyncComp('client-uploader').done(function (comp) {
                        comp.setOptions({
                            bizId: -1,
                        })
                    })
                }
            },
            deleteFormatter: function (row) {
                return row.clientName
            },
            onRowActive: function (index, row) {
                Gikam.getAsyncComp('client-uploader').done(function (comp) {
                    comp.setOptions({
                        bizId: row.id,
                    })
                })
            },
            toolbar: [{
                type: 'button',
                text: 'GIKAM.BUTTON.INSERT',
                icon: 'add',
                onClick: function () {
                    Gikam.preInsert({
                        modalTitle: 'GIKAM.AT.INSERT',
                        fields: limsClient.getBaseInfoFormFields(),
                        url: limsClient.baseUrl,
                        disableNotEmptyValidate: true
                    }).done(function (id) {
                        Gikam.getComp(_this.idManager.clientGrid).refresh()
                    })
                }
            }, {
                type: 'button',
                text: 'GIKAM.BUTTON.DELETE',
                icon: 'remove-row',
                onClick: function () {
                    Gikam.getComp(_this.idManager.clientGrid).deleteRows(limsClient.baseUrl)
                }
            }, {
                type: 'button',
                text: 'GIKAM.BUTTON.DEACTIVE',
                icon: 'disable',
                onClick: function () {
                    _this.disableClient()
                }
            }, {
                type: 'button',
                text: 'BASEMODULE.RM.FILEMANAGEMENTS.BUTTON.UPDATE_PERSON',
                icon: 'copy',
                onClick: function () {
                    _this.batchMod()
                }
            }],
            requestData: {
                clientStatus_EQ: '1'
            }
        }
    },

    getLockedGridParam: function () {
        var _this = this
        return {
            type: 'grid',
            id: _this.idManager.clientDisableGrid,
            url: limsClient.baseUrl + '/queries',
            service: 'limsClientServiceImpl',
            dbTable: 'T_LIMS_CLIENT',
            page: true,
            columns: limsClient.getEditListColumns(true),
            onLoadSuccess: function (data) {
                if (!Gikam.isEmpty(data)) {
                    this.activeRowByIndex(0)
                } else {
                    Gikam.getAsyncComp('client-uploader').done(function (comp) {
                        comp.setOptions({
                            bizId: -1,
                        })
                    })
                }
            },
            deleteFormatter: function (row) {
                return row.clientName
            },
            onRowActive: function (index, row) {
                Gikam.getAsyncComp('client-uploader').done(function (comp) {
                    comp.setOptions({
                        bizId: row.id,
                    })
                })
            },
            toolbar: [{
                type: 'button',
                text: 'GIKAM.BUTTON.ACTIVE',
                icon: 'enable',
                onClick: function () {
                    _this.enableClient()
                }
            }],
            requestData: {
                clientStatus_DIFFER: '1'
            }
        }
    },

    setUploaderReadonly: function (flag) {
        Gikam.getAsyncComp('client-uploader').done(function (uploader) {
            uploader.setToolbarVisible(!flag)
        })
    },

    getFileGrid: function () {
        var _this = this
        return {
            type: 'uploader',
            id: 'client-uploader',
            dbTable: 'T_LIMS_CLIENT',
            formatterGridColumns: function (columns) {
                return Gikam.getFormatterGridColumns(columns)
            },
            onBeforeUpload: function () {
                var selection = Gikam.getComp(_this.idManager.clientGrid).getSelections()
                if (!selection || Gikam.isEmpty(selection)) {
                    Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM', 200)
                    return false
                }
            }
        }
    },

    enableClient: function () {
        var _this = this
        Gikam.getComp(_this.idManager.clientDisableGrid).processRows({
            url: limsClient.baseUrl + '/action/enableClient',
            confirm: {
                msg: 'GIKAM.TIP.CONFIRM_TO_ACTIVATE', // 确认弹出框中左侧的信息
                title: 'GIKAM.BUTTON.ACTIVE', // 确认弹出框中的标题
            },
            formatter: function (row) {
                return row.clientName
            },
            method: 'put'
        })
    },

    disableClient: function () {
        var _this = this
        Gikam.getComp(_this.idManager.clientGrid).processRows({
            url: limsClient.baseUrl + '/action/disableClient',
            confirm: {
                msg: 'GIKAM.TIP.CONFIRM_TO_DEACTIVATE', // 确认弹出框中左侧的信息
                title: 'GIKAM.BUTTON.DEACTIVE', // 确认弹出框中的标题
            },
            formatter: function (row) {
                return row.clientName
            },
            method: 'put'
        })
    },

    batchMod: function () {
        var _this = this
        var grid = Gikam.getComp(_this.idManager.clientGrid)
        var selections = grid.getSelections()
        if (Gikam.isEmpty(selections)) {
            Gikam.alert('GIKAM.TIP.CHOOSE_ONE_ITEM')
            return
        }
        var userIds = ""
        if (selections.length === 1) {
            userIds = selections[0].personId
        }
        Gikam.create('modal', {
            title: Gikam.choose.getConfig('mult-user').title,
            url: IFM_CONTEXT + Gikam.choose.getConfig('mult-user').url + Gikam.param({
                userIds: userIds
            }),
            onAfterClose: function (rows) {
                if (Gikam.isEmpty(rows)) {
                    return
                }
                // 拼接人员ID和人员name
                var personId = '', personName = ''
                var rowlength = rows.length - 1
                Gikam.each(rows, function (index, item) {
                    if (index === rowlength) {
                        personId += item.id
                        personName += item.userName
                    } else {
                        personId += item.id + ','
                        personName += item.userName + ','
                    }
                })
                selections.forEach(s => {
                    s.personId = personId
                    s.personName = personName
                })
                workspace.window.showMask()
                Gikam.put(limsClient.baseUrl + '/update/persons',
                    Gikam.getJsonWrapper(null, ['limsClientServiceImpl', selections])).done(function () {
                    Gikam.alert('BASEDATA.MODULE.PRODUCT.CATEGORY.UPDATE.SUCCESS')
                    grid.refresh()
                }).always(function () {
                    workspace.window.closeMask()
                })
            }
        })
    },

    getMainTab: function () {
        var _this = this
        var first = false
        return {
            type: 'tab', id: 'main-tab', panels: [{
                title: 'GIKAM.ACTIVE.STATUS.YES',
                id: 'lims-client-tab-active',
                items: [_this.getGridParam()]
            }, {
                title: 'GIKAM.ACTIVE.STATUS.NO',
                id: 'lims-client-tab-locked',
                items: [_this.getLockedGridParam()]
            }],
            onActived: function (data) {
                var tab = Gikam.getComp('main-tab')
                switch (data.index) {
                    case 0:
                        if (first) {  // 首次加载页面时，不操作
                            Gikam.getAsyncComp(_this.idManager.clientGrid).done(function (grid) {
                                grid.refresh()
                            })
                            _this.setUploaderReadonly(false)
                        }
                        break
                    case 1:
                        Gikam.getAsyncComp(_this.idManager.clientDisableGrid).done(function (grid) {
                            grid.refresh()
                        })
                        _this.setUploaderReadonly(true)
                        break
                }
                first = true
            }
        }
    },

    idManager: {
        clientGrid: 'lims-client-edit-list-grid',
        clientDisableGrid: 'lims-client-edit-list-grid-locked'
    },

    create: function () {
        var _this = this
        Gikam.create('layout', {
            id: 'lims-client-edit-list-layout',
            renderTo: workspace.window.$dom,
            north: {
                height: '60%',
                items: [this.getMainTab()]
            },
            center: {
                title: 'GIKAM.FILE.ATTACHMENT',
                items: [this.getFileGrid()]
            }
        })
    },

    init: function () {
        this.create()
    }
}
